package ljl.alg.jianzhioffer.round3.easy;

public class _58_reverseWords {

    class you_dian_ben {

        public String reverseWords(String s) {
            StringBuilder sb = new StringBuilder(s.trim());
            s = sb.reverse().toString();
            String[] parts = s.split(" +");
            sb = new StringBuilder(s.length());
            for (String part : parts) {
                StringBuilder each = new StringBuilder(part);
                sb.append(each.reverse()).append(' ');
            }
            sb.setLength(sb.length() - 1);
            return sb.toString();
        }
    }

    class better {

        public String reverseWords(String s) {
            s = s.trim();
            StringBuilder res = new StringBuilder(s.length());
            String[] parts = s.split(" +");
            for (int i = parts.length - 1; i >= 0; i--) {
                res.append(parts[i]).append(' ');
            }
            return res.toString().trim();
        }
    }

}
